$(function () {
    function getTable(strategyId) {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Strategy/StrategyJQDayProfitPosition?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Strategy/StrategyJQDayProfitPosition?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
                'token': _token
            },
            data: JSON.stringify({
                "strategyId": parseInt(_urlStrategyId),
            }),
            xhrFields: {withCredentials: true},
            success: function (data) {
                console.log(data);
                var result = JSON.stringify(data);
                if (data.code == 1) {
                    var count = data.data.length;
                    $("#dailyTable tbody tr").remove();
                    for(let i=0;i<count;i++){
                      var allPositionPer =  data.data[i].allPositionPer;// (number, optional),
                        var allPositionValue =  data.data[i].allPositionValue;// (number, optional): 持仓总额 ,
                        var allPositionVolume =  data.data[i].allPositionVolume;// (integer, optional): 持仓总股数 ,
                        var allProfitValue =  data.data[i].allProfitValue;// (number, optional): 盈亏总额 ,
                        var allTodayProfitValue =  data.data[i].allTodayProfitValue;// (number, optional): 当日盈亏总额 ,
                        var dateKeyStr =  data.data[i].dateKeyStr;// (string, optional): 日期字符串 ,
                        var _length =  data.data[i].strategyPositionProfitModels.length;
                        $("#dailyTable tbody").append("<tr><td colspan='2'>日期:"+dateKeyStr+"</td></tr>");
                        if(_length==0){
                            $("#dailyTable tbody").append("<tr><td colspan='13'></td></tr>");
                        }
                        else {
                            for(let j=0;j<_length;j++){
                                var closePrice =  parseInt(data.data[i].strategyPositionProfitModels[j].closePrice*100)/100; //(number, optional): 收盘价、结算价 ,
                                var positionPer =  parseInt(data.data[i].strategyPositionProfitModels[j].positionPer*10000)/100+"%"; //(number, optional): 仓位占比 ,
                                var positionPrice =  parseInt(data.data[i].strategyPositionProfitModels[j].positionPrice*100)/100; //(number, optional): 持仓价 ,
                                var positionTradeDirection =  data.data[i].strategyPositionProfitModels[j].positionTradeDirection==1||data.data[i].strategyPositionProfitModels[j].positionTradeDirection==3?"多头":"空头"; //(string, optional): 交易方向多空 ,
                                var positionValue =  parseInt(data.data[i].strategyPositionProfitModels[j].positionValue*100)/100; //(number, optional): 市值、价值 ,
                                var positionVolume =  data.data[i]. strategyPositionProfitModels[j].positionVolume; //(integer, optional): 持仓股数 ,
                                var profitValue =  parseInt(data.data[i].strategyPositionProfitModels[j].profitValue*100)/100; //(number, optional): 盈亏 ,
                                var stockId =  data.data[i].strategyPositionProfitModels[j].stockId; //(string, optional): 股票id ,
                                var stockName =  data.data[i].strategyPositionProfitModels[j].stockName; //(string, optional): 股票名称 ,
                                var todayProfitValue =  parseInt(data.data[i].strategyPositionProfitModels[j].todayProfitValue*100)/100; //(number, optional): 当日盈亏 ,
                                var todayTradeVolume =  data.data[i].strategyPositionProfitModels[j].todayTradeVolume; //(integer, optional): 今手数 ,
                                var type =  data.data[i].strategyPositionProfitModels[j].type ;//(integer, optional): 类型
                                var tr = "<tr class='checkboxTr'><td>股票</td><td>"+stockName+"("+stockId+")</td><td>"+positionTradeDirection +"</td><td>"+positionVolume+"</td><td>"+closePrice+"</td><td>"+positionValue +"</td><td>"+profitValue +"</td><td>开仓均价</td><td>"+positionPrice +"</td><td>保证金</td><td>"+todayProfitValue +"</td><td>"+todayTradeVolume +"</td><td>"+positionPer +"</td></tr>";
                                $("#dailyTable tbody").append(tr);
                            }
                        }
                        $("#dailyTable tbody").append("<tr style='background: #F7F7F7'><td colspan='13'></td></tr>");
                    }

                    checkView()
                }
            },
            error: function () {
                alert('请求失败');
            }
        });
    }
    getTable(1001);

    function checkView() {
        $(".checkboxDiv input:checkbox").change(function () {
            var val = $(this).val();
            var check = $(this).attr('checked');
            if(check){
                $(this).removeAttr("checked");
                $(".checkboxTr").each(function () {
                    $(this).find('td').eq(val).hide();
                    $(this).find('th').eq(val).hide();
                })

            }
            else {
                $(this).attr({"checked":"checked"});
                $(".checkboxTr").each(function () {
                    $(this).find('th').eq(val).show();
                    $(this).find('td').eq(val).show();
                })
            }
        })
    }
});